﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using System.Web.Services;
using Meqs.Utilities;


public partial class Credit_Teacher_Reprots_CauseGoodStudents : ScoreInputBasePage
{
    protected List<CreditReportView> reportList = new List<CreditReportView>();
    protected List<int> causeIds = new List<int>();
    protected List<TestScoreInTermView> testScoreList = new List<TestScoreInTermView>();
    List<string[]> causeList = new List<string[]>();//课程id，课程名
    List<GlobalCauseInfo> allCauseList = new List<GlobalCauseInfo>();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void Bind()
    {
        List<CauseInfo> list = new List<CauseInfo>();
        CauseInfo item = new CauseInfo();
        item.RID = 1;
        item.SchoolMark = "School";
        list.Add(item);
        repeater1.DataSource = list;
        repeater1.DataBind();
    }


    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind();
    }

    protected string InitTableHead()
    {
        int schoolYear = 0, term = 0, grade = 0;
        string s = "";
        s = txtSchoolYear.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out schoolYear);

        s = txtTerm.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out term);

        s = txtGrade.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out grade);

        if (allCauseList == null || allCauseList.Count <= 0)
        {
            IGlobalCauseInfoLogic causeLogic = Container<IGlobalCauseInfoLogic>.Instance;
            allCauseList = (List<GlobalCauseInfo>)causeLogic.SelectSchoolCause(SchoolMark);
        }



        Hashtable ht = new Hashtable();
        ht.Add("Grade", grade);
        ht.Add("SchoolMark", SchoolMark);
        IList<CouseGradeInfo> cgList = Container<ICouseGradeInfoLogic>.Instance.FindAll(ht);

        List<GlobalCauseInfo> sl = new List<GlobalCauseInfo>();
        foreach (GlobalCauseInfo info in allCauseList)
        {
            foreach (CouseGradeInfo gradeInfo in cgList)
            {
                if (gradeInfo.CourseID == info.RID)
                {
                    sl.Add(info);
                    break;
                }
            }
        }
        allCauseList = sl;


        string str = "<tr><th>班级</th>";
        foreach (GlobalCauseInfo item in allCauseList)
        {
            str += "<th>" + item.CauseName + "</th>";
        }
        str += "<th>德育表现</th><th>综合得分</th>";
        return str + "</tr>";
    }


    protected string InitTableBody()
    {
        int schoolYear = 0, term = 0, grade = 0;
        string s = "";
        string str = "";
        s = txtSchoolYear.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out schoolYear);

        s = txtTerm.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out term);

        s = txtGrade.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out grade);

        List<CreditReportView> allCreditReport = new List<CreditReportView>();//保存所有班级所有科目的学分成绩
        IStuCausePersonCatchLogic logic = Container<IStuCausePersonCatchLogic>.Instance;
      
        IMoralityPersonCatchLogic mLogic = Container<IMoralityPersonCatchLogic>.Instance;
        List<MoralityScoreInTermView> listnn = (List<MoralityScoreInTermView>)mLogic.FindMoralityScoreInTerm(SchoolMark, schoolYear, term, grade, null);


        IClassHistoryLogic clsLogic = Container<IClassHistoryLogic>.Instance;
        List<ClsInfo> clsList = (List<ClsInfo>)clsLogic.FindClassesInSchoolYear(schoolYear, Common.GetConstSchoolMark(), grade);
     
        if (allCauseList == null || allCauseList.Count <= 0)
        {
            IGlobalCauseInfoLogic causeLogic = Container<IGlobalCauseInfoLogic>.Instance;
            allCauseList = (List<GlobalCauseInfo>)causeLogic.SelectSchoolCause(SchoolMark);
        }
        Hashtable reportData = new Hashtable ();
        Hashtable totalData = new Hashtable();
        foreach (GlobalCauseInfo globalCauseItem in allCauseList)
        { 
            int causeId = globalCauseItem.RID;
            List<CreditReportView> tempList = (List<CreditReportView>)logic.GetCreditReportView(SchoolMark, grade, schoolYear, term, causeId);
            reportData.Add(causeId, tempList);
       
            allCreditReport.AddRange(tempList);//获取所有科目所有班级的成绩list
        }

        //算班级各科最高分
        foreach (ClsInfo clsInfo in clsList)
        {
            string tr = "<tr><td>" + clsInfo.ClsName + "</td>"; 

            foreach (GlobalCauseInfo globalCauseItem in allCauseList)
            {
                List<CreditReportView> clsCauseCreditReport = (reportData[globalCauseItem.RID] as List<CreditReportView>).FindAll(delegate(CreditReportView v) { return v.ClsID == clsInfo.RID; });
                double maxscore = 0;
                string name = "";
                foreach (CreditReportView ccr in clsCauseCreditReport)
                {
                    if (maxscore <= ccr.EndScore)
                    {
                        maxscore = ccr.EndScore;
                        name += ccr.StudentName + "<br>";
                    }
                    else
                    {
                        break;            
                    }
                }
                tr += "<td>" + name + "</td>"; 
            }
            //算德育 
            double maxMorScore = 0;
            string maxMorScoreStuName = "";
            //List<CreditReportView> clsCreditReport = allCreditReport.FindAll(delegate(CreditReportView v) { return v.ClsID == clsInfo.RID; });
            //Meqs.Logic.ListSort<CreditReportView> listSort = new Meqs.Logic.ListSort<CreditReportView>(clsCreditReport, "StuID");
            //clsCreditReport = (List<CreditReportView>)listSort.Sort();

            List<MoralityScoreInTermView> clsMorList = listnn.FindAll(delegate(MoralityScoreInTermView v) { return v.ClsID == clsInfo.RID; });

            foreach (MoralityScoreInTermView view in clsMorList)
            {
                if (maxMorScore <= view.Score)
                {
                    CreditReportView stuItem = allCreditReport.Find(delegate(CreditReportView c) { return c.StuID == view.StuID; });
                    if (stuItem != null)
                    {
                        maxMorScoreStuName += stuItem.StudentName + "</br>";
                        maxMorScore = view.Score;
                    }
                }
                else
                    break;
            }
    
            tr += "<td>" + maxMorScoreStuName + "</td>";

            //算综合 学分+德育
            List<string[]> clsMaxTotalScore = new List<string[]>(); //zongfen科最高分
            double hightTotalScore = 0;
            foreach (MoralityScoreInTermView view2 in clsMorList)
            {
                double totalScore = 0;
                int stuId = view2.StuID;
                totalScore += view2.Score;
                List<CreditReportView> clsCreditReport = allCreditReport.FindAll(delegate(CreditReportView v) { return v.ClsID == clsInfo.RID && v.StuID==stuId; });
                foreach (CreditReportView view3 in clsCreditReport)
                    totalScore += view3.EndScore;

                if (hightTotalScore < totalScore)
                    hightTotalScore = Math.Round(totalScore,2);

                clsMaxTotalScore.Add(new string[] { clsCreditReport[0].StudentName, totalScore.ToString() });
            }
            string totalScoreStuName = "";
            foreach (string[] item in clsMaxTotalScore)
            {
                if (item[1] != null)
                {
                    if (Math.Round(Convert.ToDouble(item[1]), 2) == hightTotalScore)
                    {
                        totalScoreStuName += item[0] + "</br>";
                    }
                }
            }

            tr += "<td>" + totalScoreStuName + "</td>";
            str += tr; 
        }
        return str;
    }

    protected void SetGrade()
    {
        List<int> gradeList = Common.GetGradeList(SchoolCommon.Sch_SchoolInfo.SchoolOpt);
        foreach (int grade in gradeList)
        {
            string s = "";
            switch (grade)
            {
                case 1: s = "一年级"; break;
                case 2: s = "二年级";break;
                case 3: s = "三年级";break;
                case 4: s = "四年级";break;
                case 5: s = "五年级";break;
                case 6:  s = "六年级";break;
                case 7:  s = "初一";break;
                case 8:  s = "初二";break;
                case 9:  s = "初三";break;
                case 10: s = "高一"; break;
                case 11: s = "高二"; break;
                case 12: s = "高三";break;
                default: break;
            }
            Response.Write("if(document.getElementById('selGrade') != null)");
            Response.Write("{");
            Response.Write("    var selGrade =document.getElementById('selGrade'); ");
            Response.Write("    var option = document.createElement('OPTION');");
            Response.Write("    option.innerText =' "+s+"';");
            Response.Write("    option.value = "+grade+";");
            Response.Write("    selGrade.appendChild(option);");
            Response.Write("}");
        }
    }

    [WebMethod]
    public static string GetExcel(int year, int term, string clsName, ExcelTableCell[][] cells)
    {
        string name = string.Format("{0}学年{1}学期{2}", year, term == 1 ? "上学期" : "下学期", clsName);

        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "download\\" + name + "_profile.xls";
        HttpContext.Current.Session["filepath"] = save_path;
        ExcelUtil.ExcelExport(save_path, name + "优秀学生学期评比", 1, 0, cells);
        return "";
    }
}
